package cn.itcast.hotel.mq;

import cn.itcast.hotel.constants.MqConstants;
import cn.itcast.hotel.service.IHotelService;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class HotelListener {

    @Autowired
    private IHotelService hotelService;

    /**
     * 监听插入和修改
     * @param id  酒店id
     */
    @RabbitListener(bindings =@QueueBinding(
            exchange = @Exchange(value = MqConstants.HOTEL_EXCHANGE,type = ExchangeTypes.TOPIC),
            value=@Queue(name=MqConstants.HOTEL_INSERT_QUEUE),
            key = MqConstants.HOTEL_INSERT_KEY
    ))
    public void listenerInsert(Long id){
        hotelService.insertById(id);
    }


    /**
     * 监听删除
     * @param id  酒店id
     */
    @RabbitListener(bindings = @QueueBinding(
            exchange = @Exchange(value = MqConstants.HOTEL_EXCHANGE,type = ExchangeTypes.TOPIC),
            value = @Queue(name=MqConstants.HOTEL_DELETE_QUEUE),
            key = MqConstants.HOTEL_DELETE_KEY
    ))
    public void listenerDelete(Long id){
        hotelService.deleteById(id);
    }
}
